//
//  GQHFreeController.m
//  Xanthium
//
//  Created by GuanQinghao on 14/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHFreeView.h"

#pragma mark Controller
#import "GQHFreeController.h"
#import "GQHRegionController.h"
#import "GQHPushController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHFreeController () <GQHFreeViewDelegate> {
    
#pragma mark Data
    /// 发布空车信息参数
    __block NSMutableDictionary *parameters;
#pragma mark View
    /// 自定义根视图
    GQHFreeView *rootView;
}

@end


@implementation GQHFreeController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHFreeView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"发布空车";
    [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
            
        case 0: {
            /// 起始地
            GQHRegionController *regionController = [[GQHRegionController alloc] init];
            regionController.qh_regionBlock = ^(NSString *regionID, NSString *regionName, CLLocation *location)
            {
                
                if (regionID) {
                    
                    [parameters setObject:regionID forKey:@"originId"];
                    [rootView.qh_contentArray qh_safetySetObject:regionName atIndex:0];
                    [rootView.qh_tableView reloadData];
                }
            };
            [self.navigationController pushViewController:regionController animated:YES];
        }
            break;
        case 1: {
            
            /// 目的地
            GQHRegionController *regionController = [[GQHRegionController alloc] init];
            regionController.qh_regionBlock = ^(NSString *regionID, NSString *regionName, CLLocation *location)
            {
                
                if (regionID) {
                    
                    [parameters setObject:regionID forKey:@"destinationId"];
                    [rootView.qh_contentArray qh_safetySetObject:regionName atIndex:1];
                    [rootView.qh_tableView reloadData];
                }
            };
            [self.navigationController pushViewController:regionController animated:YES];
        }
            break;
           /**
        case 2: {
            
            /// 可载货物
            [self qh_alertWithTitle:@"请输入可载货物" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入可载货物";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"canCarryGoods"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:2];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 3: {
            
            /// 车辆载重
            [self qh_alertWithTitle:@"请输入车辆载重" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入车辆载重";
                textField.keyboardType = UIKeyboardTypeDecimalPad;
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (!inputTextField.text) {
                    
                    [self showToastWithText:@"车辆载重不能为空"];
                    
                    return;
                }
                
                if (!([inputTextField.text qh_isValidDecimal] || [inputTextField.text qh_isOnlyNumber])) {
                    
                    [self showToastWithText:@"车辆载重必须是整数或小数"];
                    
                    return;
                }
                
                if (0.0f == [inputTextField.text doubleValue]) {
                    
                    [self showToastWithText:@"车辆载重不能为零"];
                    
                    return;
                }
                if (inputTextField.text.length > 0) {
                    
                    NSNumber *canCarryTonnage = [NSNumber numberWithInteger:[inputTextField.text integerValue]];
                    [parameters setObject:canCarryTonnage forKey:@"canCarryTonnage"];
                    [rootView.qh_contentArray qh_safetySetObject:[NSString stringWithFormat:@"%@",canCarryTonnage] atIndex:3];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 4: {
            
            /// 备注
            [self qh_alertWithTitle:@"请输入备注" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入备注";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"note"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:4];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
            */
        default:
            break;
    }
}

#pragma mark --TargetMethod{
- (void)qh_clickedRightButton:(UIButton *)sender
{
    NSArray * keysArray = @[@"canCarryGoods", @"canCarryTonnage", @"note"];
    for (int i = 2; i < 5; i ++)
    {
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        YYEnterTextCell * cell = [rootView.qh_tableView cellForRowAtIndexPath:indexPath];
        [parameters setObject:cell.enterText.text forKey:keysArray[i - 2]];
    }
    [parameters setObject:USER_GET_ID forKey:@"token"];
    
    
    if (![parameters objectForKey:@"token"]) {
        
        [self showToastWithText:@"用户信息出错"];
        return;
    }
    
    if (![parameters objectForKey:@"originId"]) {
        
        [self showToastWithText:@"请选择起始地"];
        return;
    }
    
    if (![parameters objectForKey:@"destinationId"]) {
        
        [self showToastWithText:@"请选择目的地"];
        return;
    }
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buEmpty/1/save"];
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue])
        {
            GQHPushController *pushController = [[GQHPushController alloc] init];
            pushController.dataDic = data;
            [self.navigationController pushViewController:pushController animated:YES];
        }
        else
        {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
